﻿using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using RearLivestock.Beginning.Api.Application.Command.BreedingSow;
using RearLivestock.Common.Domains.Enum;
using RearLivestock.Common.Domains.hhx;
using RearLivestock.Common.Domains.wbx;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.ErrorCodeEnum;
using System.ComponentModel.DataAnnotations;

namespace RearLivestock.Beginning.Api.Application.Command.Farrowing
{
    public class AddFarrowingsHandler : IRequestHandler<AddFarrowingsCommmand, Result>
    {
        /// <summary>
        /// mapper
        private readonly IMapper mapper;
        /// <summary>
        /// 母猪分娩仓储
        /// </summary>
        private readonly IBaseRepository<Farrowings> farrowingsRepository;
        /// <summary>
        /// 基本信息仓储
        /// </summary>
        private readonly IBaseRepository<BasicInformation> basicInformationrepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<AddFarrowingsHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="farrowingsRepository"></param>
        public AddFarrowingsHandler(IMapper mapper, IBaseRepository<Farrowings> farrowingsRepository, IBaseRepository<BasicInformation> basicInformationrepository,ILogger<AddFarrowingsHandler> logger)
        {
            this.mapper = mapper;
            this.farrowingsRepository = farrowingsRepository;
            this.basicInformationrepository = basicInformationrepository;
            this.logger = logger;
        }
        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(AddFarrowingsCommmand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            try
            {
                foreach (var item in request.Farrowings)
                {
                    //根据前台传入的耳号id进行查询
                    var list = await (await basicInformationrepository.GetAll()).Where(d => d.BasicInforId == item.BasicInforId).ToListAsync();
                    //循环修改猪只档案(基本信息)种公的栋舍 栏位 在场状态 入场时间
                    list.ForEach(d =>
                    {
                        d.BedroomId = item.BedroomId;
                        d.FieldId = item.FieldId;
                        d.Presentstatus = PresentstatusEnums.在场;
                        d.EntryDate = DateTime.Now;
                    });
                    //修改猪只档案信息
                    basicInformationrepository.UpdRange(list);
                }
                var res = farrowingsRepository.AddRange(request.Farrowings);
                if (res > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "母猪分娩添加成功";
                }
            }
            catch (Exception ex)
            {
                result.Msg = "母猪分娩信息添加异常";
                logger.LogError("母猪分娩信息添加异常",ex.Message);
            }
            return result;
        }
    }
}
